package com.definesys.smecrailtransit.module.SiteDetails.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.databinding.DialogSiteErrorBinding;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsRealTimeAdapter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.ELeStatusBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleErrorContentBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.util.ArrayList;

import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRealTimeFragment.GET_ELE_STATUS_SUCCESS;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRealTimeFragment.GET_ERROR_CONTENT_SUCCESS;


/**
 * Created by xupeizuo on 2017/8/28.
 */

public class SiteReaErrorDialog extends Dialog{

    private Context mContext;
    public DialogSiteErrorBinding dialogSiteErrorBinding;
    private int showOnceDialog=0;
    private EleErrorContentBean eleErrorContentBean;
    private SiteDetailsRealTimeAdapter siteDetailsRealTimeAdapter;
    private SiteDetailsPresenter siteDetailsPresenter;
    protected Bus mRxBus;
    private String stationName;

    public void setStationName(String stationName) {
        this.stationName = stationName;
        dialogSiteErrorBinding.errorSite.setStationName(stationName);
    }

    public SiteReaErrorDialog(Context context, int themeResId, boolean isOnce, SiteDetailsPresenter presenter) {
        super(context,themeResId);
        this.mContext=context;
        this.setCanceledOnTouchOutside(true);
        eleErrorContentBean=new EleErrorContentBean();
        dialogSiteErrorBinding=DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.dialog_site_error,null,false);
        dialogSiteErrorBinding.errorContent.setContent(eleErrorContentBean);
        this.showOnceDialog=isOnce ? 0 :1;
        siteDetailsPresenter=presenter;
        mRxBus= SmecRxBus.get();
        mRxBus.register(this);
    }

    @Override
    public void dismiss() {
        CommonUtils.cardFront(dialogSiteErrorBinding.errorDialog,R.id.error_site,R.id.error_content);
        super.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogSiteErrorBinding.getRoot());
        initView();
    }

    public void initView(){
        CommonUtils.setCameraDistance(mContext.getResources().getDisplayMetrics(),dialogSiteErrorBinding.errorDialog);
        if(showOnceDialog == 1){
            dialogSiteErrorBinding.site.setVisibility(View.VISIBLE);
            dialogSiteErrorBinding.errorContent.windowBack.setVisibility(View.VISIBLE);
            siteDetailsRealTimeAdapter=new SiteDetailsRealTimeAdapter(mContext,siteDetailsPresenter,showOnceDialog,this);
            dialogSiteErrorBinding.errorSite.realTimeRecycle.setLayoutManager(new LinearLayoutManager(mContext));
            dialogSiteErrorBinding.errorSite.realTimeRecycle.setAdapter(siteDetailsRealTimeAdapter);
            dialogSiteErrorBinding.errorContent.windowBack.setOnClickListener(v ->
                    CommonUtils.cardFront(dialogSiteErrorBinding.errorDialog,R.id.error_site,R.id.error_content));
        }
    }
    public void setEleErrorContentBean(EleErrorContentBean eleErrorContentBean) {
        if(eleErrorContentBean == null){
            return;
        }
        this.eleErrorContentBean = eleErrorContentBean;
        dialogSiteErrorBinding.errorContent.setContent(eleErrorContentBean);
        dialogSiteErrorBinding.invalidateAll();
    }

    public DialogSiteErrorBinding getDialogSiteErrorBinding() {
        return dialogSiteErrorBinding;
    }

    public SiteDetailsRealTimeAdapter getSiteDetailsRealTimeAdapter() {
        return siteDetailsRealTimeAdapter;
    }

    public void frontToOpposite(){
        CommonUtils.cardBack(dialogSiteErrorBinding.errorDialog,
                dialogSiteErrorBinding.errorContent.errorContenItem,R.id.error_site,R.id.error_content);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ELE_STATUS_SUCCESS)
            }
    )
    public void getELeStatusSuccess(ArrayList<ELeStatusBean> arrayList){
        if(siteDetailsRealTimeAdapter == null){
            return;
        }
        siteDetailsRealTimeAdapter.setArrayList(arrayList);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ERROR_CONTENT_SUCCESS)
            }
    )
    public void getErrorContent(EleErrorContentBean eleErrorContentBean){
        setEleErrorContentBean(eleErrorContentBean);
    }
}
